package com.ddstation.waste_delivery.service;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import com.ddstation.app.holiday.service.HolidayService;
import com.ddstation.crud.entity.DtInvsIllegalCheckDailyEntity;
import com.ddstation.crud.mapper.DtInvsIllegalCheckDailyMapper;
import com.ddstation.home_page.service.DtInvsMessageQueueService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ddstation.app.waste_delivery.model.AppWasteDeliveryResModel;
import com.ddstation.common.model.InvsCheckCommonSchParModel;
import com.ddstation.crud.entity.DtInvsWasteDeliveryDetailEntity;
import com.ddstation.crud.entity.DtInvsWasteDeliveryEntity;
import com.ddstation.crud.mapper.DtInvsWasteDeliveryDetailMapper;
import com.ddstation.crud.mapper.DtInvsWasteDeliveryMapper;
import com.ddstation.waste_delivery.mapper.WasteDeliveryMapper;
import com.ddstation.waste_delivery.model.WasteDeliveryDetailParModel;
import com.ddstation.waste_delivery.model.WasteDeliveryHeadResModel;
import com.ddstation.waste_delivery.model.WasteDeliveryResModel;

@Service
public class WasteDeliveryServiceImpl implements WasteDeliveryService {
	// 查询
	@Override
	@Transactional(readOnly=true)
	public List<WasteDeliveryResModel> searchWasteDeliveryList(InvsCheckCommonSchParModel _in){
        return wasteDeliveryMapper.selectWasteDeliveryList(_in);
	}
	
	// 生成诊所登记明细报表
	@Override
	@Transactional(readOnly=true)
	public List<WasteDeliveryHeadResModel> searchInstitutionSewageDetailList(WasteDeliveryDetailParModel _in){
		// 查询check数据
		return wasteDeliveryMapper.selectDetail(_in);
	}

	@Override
	public List<AppWasteDeliveryResModel> selectByHistory(String institutionId, int pageNo, int pageSize) {
		int startNo = (pageNo - 1) * pageSize;
		return wasteDeliveryMapper.selectByHistory(institutionId,startNo,pageSize);
	}

	@Override
	@Transactional(rollbackFor=Exception.class)
	public Map<String, Object> addWasteDelivery(String institutionId, Integer doctorId, DtInvsWasteDeliveryEntity entity, List<DtInvsWasteDeliveryDetailEntity> detailEntityList) {
		Map<String,Object> map = new HashMap<String, Object>();
		dtInvsWasteDeliveryMapper.insert(entity);
		map.put("info",entity);
		for (DtInvsWasteDeliveryDetailEntity detailEntity : detailEntityList) {
			detailEntity.setDeliveryId(entity.getId());
		}
		dtInvsWasteDeliveryDetailMapper.insertList(detailEntityList);

		//新增消息
		dtInvsMessageQueueService.insertMessageQueue(institutionId,201,1,1,entity.getId().toString(),1);

		// 更新卫监日常记录
		Date today = new Date();
		List<DtInvsIllegalCheckDailyEntity> checkList = dtInvsIllegalCheckDailyMapper.selectOneByDateAndInstitution(today, entity.getInstitutionId());
		if (null != checkList && !checkList.isEmpty()) {
			for (DtInvsIllegalCheckDailyEntity ele: checkList) {
				if (ele.getBusinessSubType() == 201) {
					ele.setTargetId(entity.getId().toString());//取最近一次登记的记录
					dtInvsIllegalCheckDailyMapper.updateByPrimaryKeySelective(ele);

					map.put("errCode", 0);
					map.put("errormsg", "");
					return map;
				}
			}
		}
		DtInvsIllegalCheckDailyEntity _checkRet1 = new DtInvsIllegalCheckDailyEntity();// 一条check结果
		_checkRet1.setInstitutionId( entity.getInstitutionId());//机构ID
		_checkRet1.setCheckDt(today);//检查日期
		_checkRet1.setBusinessType(20);//'@10：消毒登记 @20：医废登记 @30：检测审查 @40：处方违规',
		_checkRet1.setBusinessSubType(201);//'子分类 @102：医废
		_checkRet1.setAmPm(0);
		String dateStr = DateTimeFormatter.ofPattern("yyyy-MM-dd").format(LocalDateTime.ofInstant(today.toInstant(), ZoneId.systemDefault()));
		if (holidayService.checkHoliday(dateStr, entity.getInstitutionId())) {
			_checkRet1.setMsgId("900007");
			_checkRet1.setMessage("休息日");
		}
		_checkRet1.setCheckSt(0);//正常
		_checkRet1.setTargetId(entity.getId().toString());//取最近一次登记的记录
		dtInvsIllegalCheckDailyMapper.insert(_checkRet1);

		return map;
	}

	@Override
	public DtInvsWasteDeliveryEntity wasteDeliveryDetail(String institutionId, Integer id) {

		return dtInvsWasteDeliveryMapper.selectForDetail(id,institutionId);
	}


	@Inject private DtInvsMessageQueueService dtInvsMessageQueueService;
	@Inject private WasteDeliveryMapper wasteDeliveryMapper;
	@Inject private DtInvsWasteDeliveryMapper dtInvsWasteDeliveryMapper;
	@Inject private DtInvsWasteDeliveryDetailMapper dtInvsWasteDeliveryDetailMapper;
	@Inject private DtInvsIllegalCheckDailyMapper dtInvsIllegalCheckDailyMapper;
	@Inject private HolidayService holidayService;
}
